let provinceCityArea = {
  placesMap: {},
  provinceArr: [],
  cityArr: [],
  areaArea: [],
  provinces: null,
  cities: null,
  districts: null,
  Place: function (AreaCode, Name) {
    this.AreaCode = AreaCode
    this.Name = Name
    this.Place.prototype.PlaceType = function () {
      let ac = parseInt(this.AreaCode)
      if (ac % 100 !== 0) {
        return 'd'
      } else if (ac % 10000 !== 0) {
        return 'c'
      } else {
        return 'p'
      }
    }
    this.Place.prototype.ProvinceCode = function () {
      // 整除10000得到省级编码（前2位数字）
      let ac = parseInt(this.AreaCode)
      return Math.floor(ac / 10000)
    }
    /* 返回地点所属市编码 */
    this.Place.prototype.CityCode = function () {
      // 整除100得到市级编码（前4位数字）
      let ac = parseInt(this.AreaCode)
      return Math.floor(ac / 100)
    }
  },
  initSeletList: function () {
    // 遍历placesMap这个Json对象,根据key：value对创建place对象，并根据地区类型分类
    for (let key in this.placesMap) {
      let pl = new this.Place(key, this.placesMap[key])
      let ty = pl.PlaceType()
      if (ty === 'p') {
        this.provinces.push(pl)
      }
      if (ty === 'c') {
        this.cities.push(pl)
      }
      if (ty === 'd') {
        this.districts.push(pl)
      }
    }
    // 初始化省下拉选择列表
    for (let i = 0; i < this.provinces.length; i++) {
      let obj = {}
      obj.label = this.provinces[i].Name
      obj.value = this.provinces[i].ProvinceCode()
      this.provinceArr.push(obj)
    }
    console.log(this.provinceArr)
  },
  changeSelect: function (element) {
    let id = element.getAttribute('id')
    /* 省下拉列表改变时更新市下拉列表和区下拉列表 */
    if (id === 'province' && element.value !== '000000') {
      document.getElementById('city').options.length = 1// 清除市下拉列表旧选项
      let pCode = parseInt(element.value)// 获取省下拉列表被选取项的省编码
      /* 根据省编码更新市下拉列表 */
      if (pCode !== 0) {
        for (let i = 0; i < this.cities.length; i++) {
          if (this.cities[i].ProvinceCode() === pCode) {
            let obj = {}
            obj.label = this.cities[i].Name
            obj.value = this.cities[i].CityCode()
            this.cityArr.push(obj)
          }
        }
      }
      document.getElementById('district').options.length = 1// 清除区下拉列表旧选项
      let cCode = parseInt(document.getElementById('city').value)// 获取市下拉列表被选中项的市编码
      /* 根据市编码更新区下拉列表 */
      if (cCode !== 0) {
        for (let i = 0; i < this.districts.length; i++) {
          if (this.districts[i].CityCode() === cCode) {
            let obj = {}
            obj.label = this.districts[i].Name
            obj.value = this.districts[i].AreaCode
            this.areaArr.push(obj)
          }
        }
      }
    }

    /* 市下拉列表改变时更新区下拉列表的选项 */
    if (id === 'city' && element.value !== '000000') {
      document.getElementById('district').options.length = 1// 清除区下拉列表旧选项
      let cCode = parseInt(element.value)// 获取市下拉列表被选中项的市编码
      /* 根据市编码更新区下拉列表 */
      for (let i = 0; i < this.districts.length; i++) {
        if (this.districts[i].CityCode() === cCode) {
          let obj = {}
          obj.label = this.districts[i].Name
          obj.value = this.districts[i].AreaCode
          this.areaArr.push(obj)
        }
      }
    }
  }
}

export default provinceCityArea
